library(haven)
library(tidyverse)

data_22 <- read_xpt('LLCP2022.XPT_')

view(colnames(data_22))
table(data_22$`_AGE80`)
table(data_22$SEXVAR)

mean(data_22$TRNSGNDR > 5, na.rm = T)
sum(data_22$TRNSGNDR > 5, na.rm = T)
sum(is.na(data_22$TRNSGNDR))

df.22 <- data_22 %>%
  filter(TRNSGNDR < 5)
df.22$age <- df.22$'_AGE80'

mean(df.22$age == 80, na.rm = T)
sum(df.22$age == 80, na.rm = T)

df.22 <- df.22 %>%
  filter(age < 80) %>%
  mutate(trans = if_else(TRNSGNDR < 4, 1, 0),
         transfem = if_else(TRNSGNDR == 1, 1, 0),
         transmasc = if_else(TRNSGNDR == 2, 1, 0),
         gnc = if_else(TRNSGNDR == 3, 1, 0),
         gender_id = case_when(TRNSGNDR == 1 ~ "Transfeminine",
                               TRNSGNDR == 2 ~ "Transmasculine",
                               TRNSGNDR == 3 ~ "Gender nonconforming",
                               TRNSGNDR == 4 & SEXVAR == 1 ~ "Cis man",
                               TRNSGNDR == 4 & SEXVAR == 2 ~ "Cis woman"))

#Age distributions

df.22 %>% #all participants
  ggplot() +
  aes(x = age) +
  geom_histogram(binwidth = 2, col = "deepskyblue4", fill = "deepskyblue3")

df.22 %>% #trans participants
  filter(trans == 1) %>%
  ggplot() +
  aes(x = age) +
  geom_histogram(binwidth = 2, col = "deepskyblue4", fill = "deepskyblue3")

#Clean covariates
df.22$race2 <- df.22$`_MRACE2`
df.22$ethnicity2 <- df.22$`_HISPANC`
df.22 <- df.22 %>%
  mutate(ethnicity = case_when(ethnicity2 == 1 ~ "Hispanic",
                               ethnicity2 == 2 ~ "Non-Hispanic"),
         
         race = case_when(race2 == 1 ~ "White",
                          race2 == 2 ~ "Black",
                          race2 %in% c(3,5) ~ "Other",
                          race2 == 4 ~ "Asian",
                          race2 == 6 ~ "Multiracial"))

df.22 <- df.22 %>%
  mutate(edu = case_when(EDUCA < 4 ~ "Less than HS",
                         EDUCA < 5 ~ "HS",
                         EDUCA < 6 ~ "Some college",
                         EDUCA == 6 ~ "College degree"))
         

#Clean health outcomes
df.22 <- df.22 %>%
  mutate(fair_poor = case_when(GENHLTH < 4 ~ 0,
                               GENHLTH < 6 ~ 1))
df.22$trans_fac <- factor(df.22$trans,
                          levels = c(0,1),
                          labels = c("Cis", "Trans"))

df.22$weights <- df.22$'_LLCPWT'

df.22$id <- df.22$'_PSU'

df.22$state_id <- df.22$'_STATE'

df.22 <- df.22 %>%
  mutate(diabetes = case_when(DIABETE4 == 1 ~ 1,
                              DIABETE4 %in% c(2:4) ~ 0),
         diabetes_type = case_when(DIABTYPE == 1 ~ 1,
                                   DIABTYPE == 2 ~ 2),
         diabetes_early = case_when(DIABAGE4 < 25 ~ 1,
                                    DIABAGE4 %in% c(26:80) ~ 0),
         depression = case_when(ADDEPEV3 == 1 ~ 1,
                                ADDEPEV3 == 2 ~ 0),
         arthritis = case_when(HAVARTH4 == 1 ~ 1,
                               HAVARTH4 == 2 ~ 0))


df.22$blind <- case_when(df.22$BLIND == 2 ~ 0,
                         df.22$BLIND == 1 ~ 1)
df.22$deaf <- case_when(df.22$DEAF == 2 ~ 0,
                        df.22$DEAF == 1 ~ 1)
df.22$decide <- case_when(df.22$DECIDE == 2 ~ 0,
                          df.22$DECIDE == 1 ~ 1)
df.22$walk <- case_when(df.22$DIFFWALK == 2 ~ 0,
                        df.22$DIFFWALK == 1 ~ 1)
df.22$dress <- case_when(df.22$DIFFDRES == 2 ~ 0,
                         df.22$DIFFDRES == 1 ~ 1)
df.22$alone <- case_when(df.22$DIFFALON == 2 ~ 0,
                         df.22$DIFFALON == 1 ~ 1)

df.22$disabled <- if_else(df.22$blind + df.22$deaf + df.22$decide + df.22$walk + df.22$dress + df.22$alone > 0, 1, 0)

#view(colnames(df.22))
  
clean_22 <- df.22 %>%
  select(SEXVAR, HTIN4, age:disabled)

clean_22 <- clean_22 %>%
  mutate(year = 2022)

write_csv(clean_22, "Clean data 2022.csv")

